package com.hljcj.boot.action.sysset;

import com.hljcj.boot.bean.SysDepart;
import com.hljcj.boot.service.DepartService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/sysset")
public class DepartAction {

    @Resource
    private DepartService departService;

    @RequestMapping(value = "/initDepart")
    public ModelAndView initDepart() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sysset/depart.html");
        return modelAndView;
    }

    @RequestMapping(value = "/queryDepart", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> queryDepart(SysDepart depart, int pageNo, int maxResults) {
        Map<String, Object> map = new HashMap<>();
        try {
            Page<SysDepart> page = departService.queryDepart(depart, pageNo, maxResults);
            map.put("total", page.getTotalElements());
            map.put("rows", page.getContent());
        } catch (Exception e) {
            e.printStackTrace();
            map.put("total", 0);
            map.put("rows", null);
        }
        return map;
    }

    @RequestMapping(value = "/addDepart", method = {RequestMethod.POST, RequestMethod.GET})
    public Map<String, String> addDepart(SysDepart depart) {
        Map<String, String> map = new HashMap<>();
        try {
            String mess = departService.addDepart(depart);
            map.put("mess", mess);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("mess", e.toString());
        }
        return map;
    }


    @RequestMapping(value = "/updDepart", method = {RequestMethod.POST, RequestMethod.GET})
    public Map<String, String> updDepart(SysDepart depart) {
        Map<String, String> map = new HashMap<>();
        try {
            String mess = departService.updDepart(depart);
            map.put("mess", mess);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("mess", e.toString());
        }
        return map;
    }


    @RequestMapping(value = "/delDepart", method = {RequestMethod.POST})
    public Map<String, String> delDepart(Integer departId) {
        Map<String, String> map = new HashMap<>();
        try {
            departService.delDepart(departId);
            map.put("mess", "succ");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("mess", e.toString());
        }
        return map;
    }

}
